package com.jxl.application.controller.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jxl.application.controller.convert.SubjectCategoryConvert;
import com.jxl.application.controller.convert.SubjectLabelConvert;
import com.jxl.application.controller.dto.SubjectCategoryDto;
import com.jxl.application.controller.dto.SubjectLabelDto;
import com.jxl.common.entity.Result;
import com.jxl.domain.entity.SubjectCategoryBO;
import com.jxl.domain.entity.SubjectLabelBO;
import com.jxl.domain.service.SubjectCategoryDomainService;
import com.jxl.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RequestMapping("/subject/label")
@RestController
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 标签添加
     * @param subjectLabelDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDto subjectLabelDto){
        try{
            if (log.isInfoEnabled()){ //并发问题解决
                log.info("参数是{}", JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getLabelName(),"标签名称不能为空");//guava判断值能不能为空
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.
                    INSTANCE.convertDtoToSubjectLabelBO(subjectLabelDto);
            subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(true);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 更新分类
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDto subjectLabelDto){
        try{
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.
                    INSTANCE.convertDtoToSubjectLabelBO(subjectLabelDto);
            Boolean update = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(update);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除分类
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDto subjectLabelDto){
        try{
            Boolean update = subjectLabelDomainService.delete(subjectLabelDto.getId());
            return Result.ok(update);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 根据分类查询标签
     * @return
     */
    @PostMapping("/queryByCategory")
    public Result<List<SubjectLabelDto>> queryByCategory(@RequestBody SubjectLabelDto subjectLabelDto){
        try{
            SubjectLabelBO subjectLabelBO =
                    SubjectLabelConvert.INSTANCE.convertDtoToSubjectLabelBO(subjectLabelDto);
            List<SubjectLabelBO> labelBOS = subjectLabelDomainService.queryByCategory(subjectLabelBO);
            List<SubjectLabelDto> subjectLabelDtos =
                    SubjectLabelConvert.INSTANCE.convertBOToSubjectLabelDtoList(labelBOS);
            return Result.ok(subjectLabelDtos);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }
}
